package com.admissions.dapan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.admissions.AdmissionsActivity;
import com.admissions.BaseActivity;
import com.admissions.R;
import com.admissions.adapter.DapAnAdapter;
import com.admissions.adapter.DepartmentAdapter;
import com.admissions.adapter.ExamAdapter;
import com.admissions.common.Constants;
import com.admissions.data.DataPaser;
import com.admissions.model.DapAnMonThiModel;
import com.admissions.model.DepartmentModel;
import com.admissions.model.ExamModel;
import com.admissions.network.HttpNetService;

public class DapAnDeThi extends BaseActivity {
	private Resources mRes;
	public static TextView tvXemTheoMon;
	public static TextView tvXemTheoKhoi;
	private Button btnXem;
	private List<ExamModel> listExam;
	public static AlertDialog alertDialog;
	public static String khoiThiId;
	public static String monThiId;
	private List<DapAnMonThiModel> listAnswer;

	private Map<String, List<DepartmentModel>> departmentMap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mRes = getResources();

	}

	public void initLayout() {
		tvXemTheoMon = (TextView) findViewById(R.id.tv_xem_theo_mon);
		tvXemTheoKhoi = (TextView) findViewById(R.id.tv_xem_theo_khoi);
		btnXem = (Button) findViewById(R.id.btn_xem);

		tvXemTheoMon.setOnClickListener(this);
		tvXemTheoKhoi.setOnClickListener(this);
		btnXem.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		setContentView(R.layout.dap_an_de_thi);

		initLayout();
		khoiThiId = null;
		monThiId = null;
		// Setup Header
		String headerContent = mRes.getString(R.string.header_dap_an_de_thi);
		String headerComment = mRes.getString(R.string.header_comment_dap_an_de_thi);
		Drawable icon = mRes.getDrawable(R.drawable.ic_dap_an_selected);
		((AdmissionsActivity) getParent()).setupHeader(icon, headerContent, headerComment);

		DownloadData data = new DownloadData(this);
		data.execute();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_xem_theo_khoi:
			LayoutInflater layoutInflater = LayoutInflater.from(this);
			View layoutList = layoutInflater.inflate(R.layout.list, null);
			ListView listItem = (ListView) layoutList.findViewById(R.id.listview);
			ExamAdapter examAdapter = new ExamAdapter(this, listExam);
			listItem.setAdapter(examAdapter);
			alertDialog = new AlertDialog.Builder(this).setIcon(R.drawable.icon).setTitle("Chọn khối thi").setView(layoutList).create();
			alertDialog.show();
			break;

		case R.id.tv_xem_theo_mon:
			layoutInflater = LayoutInflater.from(this);
			layoutList = layoutInflater.inflate(R.layout.list, null);
			listItem = (ListView) layoutList.findViewById(R.id.listview);

			List<DepartmentModel> departmentModels = new ArrayList<DepartmentModel>();

			try {
				departmentModels = departmentMap.get(khoiThiId);
			} catch (Exception e) {
			}
			DepartmentAdapter departmentAdapter = new DepartmentAdapter(this, departmentMap.get(khoiThiId));
			listItem.setAdapter(departmentAdapter);
			alertDialog = new AlertDialog.Builder(this).setIcon(R.drawable.icon).setTitle("Chọn môn thi").setView(layoutList).create();
			alertDialog.show();
			break;
		case R.id.btn_xem:
			DownloadDapAn downloadDapAn = new DownloadDapAn(this, this);
			downloadDapAn.execute();
			break;
		default:
			break;
		}
	}

	private class DownloadData implements Constants {
		private Context context;
		private ProgressDialog dialog;

		public DownloadData(Context c) {
			context = c;
		}

		private void execute() {
			new ShowItems().execute(new Void[] { null });
		}

		public class ShowItems extends AsyncTask<Void, Void, Boolean> {
			public ShowItems() {
			}

			@Override
			protected Boolean doInBackground(Void... arg0) {
				listExam = DataPaser.getListKhoiThiCoDapAn(HttpNetService.getJsonByURL(URL_LIST_KHOI_THI_CO_DAP_AN));

				for (ExamModel exam : listExam) {
					if (departmentMap == null) {
						departmentMap = new HashMap<String, List<DepartmentModel>>();
					}
					departmentMap.put(exam.getId(), DataPaser.getListMonThiCoDapAn(HttpNetService.getJsonByURL(URL_LIST_MON_THI_CO_DAP_AN + exam.getId())));
				}
				return true;
			}

			@Override
			protected void onPreExecute() {
				dialog = new ProgressDialog(context);
				dialog.setCancelable(true);
				dialog.setMessage("Đang tải...");
				dialog.show();
			}

			@Override
			protected void onPostExecute(Boolean result) {
				if (dialog.isShowing() == true) {
					if (result == true) {

					} else {
						showDialogWithInvalidConnection();
					}

				}
				dialog.dismiss();
			}

		}

	}

	private class DownloadDapAn implements Constants {
		private Context context;
		private Activity activity;
		private ProgressDialog dialog;

		public DownloadDapAn(Context c, Activity activity) {
			context = c;
			this.activity = activity;
		}

		private void execute() {
			new ShowItems().execute(new Void[] { null });
		}

		public class ShowItems extends AsyncTask<Void, Void, Boolean> {
			public ShowItems() {
			}

			@Override
			protected Boolean doInBackground(Void... arg0) {
				listAnswer = DataPaser.getListDapAnCuaMonThi(HttpNetService.getJsonByURL(URL_LIST_DAP_AN_CUA_MON_THI + khoiThiId + "/" + monThiId));

				return true;
			}

			@Override
			protected void onPreExecute() {
				dialog = new ProgressDialog(context);
				dialog.setCancelable(true);
				dialog.setMessage("Đang tải...");
				dialog.show();
			}

			@Override
			protected void onPostExecute(Boolean result) {
				boolean isShowing = dialog.isShowing();
				dialog.dismiss();
				if (isShowing) {
					if (result == true) {

						LayoutInflater layoutInflater = LayoutInflater.from(context);
						View layoutList = layoutInflater.inflate(R.layout.list, null);
						ListView listItem = (ListView) layoutList.findViewById(R.id.listview);
						DapAnAdapter dapAnAdapter = new DapAnAdapter(activity, listAnswer);
						listItem.setAdapter(dapAnAdapter);

						alertDialog = new AlertDialog.Builder(context).setIcon(R.drawable.icon).setTitle("Đáp án môn thi").setView(layoutList).create();
						alertDialog.show();

					} else {
						showDialogWithInvalidConnection();
					}

				}

			}

		}

	}

}
